package
{
	import as3isolib.display.IsoSprite;
	import as3isolib.display.IsoView;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.scene.IsoGrid;
	import as3isolib.display.scene.IsoScene;
	import as3isolib.geom.IsoMath;
	import as3isolib.geom.Pt;
	
	import flash.display.DisplayObject;
	import flash.display.Scene;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	public class Game extends Sprite
	{
		public static const TERRAIN_TILE_SIZE:int = 4*Game.PPS;
		public static const SECTOR_SIZE:int = Game.PPS*2;
		public static const PPS:Number = 250;
		public static var stage:Stage;
		public static var instance:Game;

		private var _view:IsoView;
		private var objectScene:IsoScene;
		private var _box:IsoBox;
		private var _grid:IsoGrid;
		private var terrainController:TerrainController;
		private var sectorController:SectorController;
		
		public function Game()
		{
			setupScene();
			setupInputHandlers();
		}
		
		private function setupScene():void{
			
			
			var stage:Stage = Game.stage;
			
			terrainController = new TerrainController(this);
			
				
			_view = new IsoView();
			_view.setSize( stage.stageWidth, stage.stageHeight );
			addChild( _view );

			
			
			_view.addScene(terrainController.scene);

			
			objectScene = new IsoScene();
			objectScene.hostContainer = this;
			_view.addScene( objectScene );
			
			sectorController = new SectorController(objectScene);
					
			
			_box = new IsoBox();
			_box.setSize( PPS/10, PPS/10, PPS/10);
			objectScene.addChild( _box );
			_box.autoUpdate = true;
			
			_grid = new IsoGrid();
			_grid.setGridSize(20,20);
			_grid.cellSize = PPS;
			objectScene.addChild( _grid );	
			
			
			objectScene.render(true);
			
		}
		
		
		
		public static function createIsoSprite(url:String, width:int=0, height:int=0):IsoSprite{
			var isoSprite:IsoSprite = new IsoSprite();
			var graphic:DisplayObject = DisplayObjectLoader.load(url, width, height, function(image:DisplayObject):void{
				image.x = -image.width/2;
				image.y = -image.height;
			});
			
			//graphic.x = -width/2;
			isoSprite.container.addChild(graphic);
			isoSprite.setSize(50,50,1);
			return isoSprite;
		}
		
		private function setupInputHandlers():void{
			var stage:Stage = Game.stage;			
			var downPoint:Point;
			var totalDragDistance:Number = 0;
			var finalDestination:Point = new Point(_box.x, _box.y);
			var autoPan:Boolean = true;
			stage.addEventListener(MouseEvent.MOUSE_DOWN, function(me:MouseEvent):void{
				downPoint = new Point(me.stageX, me.stageY);
			});
			
			stage.addEventListener(MouseEvent.MOUSE_UP, function(me:MouseEvent):void{
				if(totalDragDistance <= 1){
					finalDestination = _view.localToIso(downPoint);
					var boxPos:Point = new Point(_box.x, _box.y);
					var delta:Point = finalDestination.subtract(boxPos);
					
					var clampedFinalDestination:Point = new Point(Math.floor(finalDestination.x), Math.floor(finalDestination.y));
				}
				totalDragDistance = 0;
				downPoint = null;
				
			});				
			stage.addEventListener(MouseEvent.MOUSE_MOVE, function(me:MouseEvent):void{
				if(downPoint){
					var curpoint:Point = new Point(me.stageX, me.stageY);
					var delta:Point = downPoint.subtract(curpoint);
					totalDragDistance += delta.length;
					_view.panBy(delta.x, delta.y);
					downPoint = curpoint;
					autoPan = false;
					var centerPoint:Point = _view.localToIso(new Point(stage.stageWidth/2, stage.stageHeight/2));
					terrainController.setPosition(centerPoint);
					sectorController.setPosition(centerPoint);
				}
			});				
		}
		
		public function renderObjects():void{
			
			
			var i:int, j:int;
			var tree:IsoSprite;
			for(i = 0; i < 5000; i += 50){
				for(j = 0; j < 5000; j+= 50){
					if(Math.floor(Math.random()*100) == 1){
						tree = createIsoSprite('assets/redwood.png');
						tree.x = i;
						tree.y = j;
						objectScene.addChild(tree);
					}
				}
			}
		}
		
		public static function clampPoint(point:Point, amount:int):Point{
			return new Point(Math.floor(point.x/amount)*amount, Math.floor(point.y/amount)*amount);
		}
		
		public static function debugPoint(point:Point):void{
			trace("p: [" + point.x + "," + point.y + "]");
		}
		
		public static function getGrassTerrainTile():Sprite{
			var size:int = TERRAIN_TILE_SIZE/Game.PPS;
			var i:int, j:int;
			var container:Sprite = new Sprite();
			for(i = 0; i < size; i++){
				for(j = 0; j < size; j++){
					var grass:DisplayObject = DisplayObjectLoader.load('assets/landYellow.png',500,250);
					var screen:Pt = IsoMath.isoToScreen(new Pt(i*Game.PPS, j*Game.PPS));
					grass.x = screen.x - grass.width/2;
					grass.y = screen.y;
					container.addChild(grass);
				}
			}
			
			return container;
		}
	}
}